package com.zhang.learn.javaassist.service;

import com.zhang.learn.javaassist.dto.FileInfoDto;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author zhanghaolian
 * @date 2021/7/21 22:42
 */
@Component
public class WordProcesser {
    /**
     * 获取word文件的总页数
     *
     * @param fileInfoDto
     * @throws IOException
     */
    public void getPageCount(FileInfoDto fileInfoDto) {
        try {
            if (fileInfoDto.getSuffix().equals("doc")) {
                FileInputStream fis = new FileInputStream(fileInfoDto.getSysPath());
                HWPFDocument document = new HWPFDocument(fis);
                int pages = document.getSummaryInformation().getPageCount();//总页数
                fileInfoDto.setPageCount(pages);
                fis.close();
            } else if (fileInfoDto.getSuffix().equals("docx")) {
                FileInputStream fis = new FileInputStream(fileInfoDto.getSysPath());
                XWPFDocument document = new XWPFDocument(fis);
                int pages = document.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
                fileInfoDto.setPageCount(pages);
                fis.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
